home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DMAKE38B.ARJ / TESTMLC.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  4KB  |  177 lines

  1. /* NOT copyright by SoftQuad Inc. -- msb, 1988 */
  2. #ifndef lint
  3. static char *SQ_SccsId = "@(#)mtest3.c    1.2 88/08/25";
  4. #endif
  5. #include <stdio.h>
  6. /*
  7. ** looptest.c -- intensive allocator tester 
  8. **
  9. ** Usage:  looptest
  10. **
  11. ** History:
  12. **    4-Feb-1987 rtech!daveb 
  13. */
  14.  
  15. # ifdef SYS5
  16. # define random    rand
  17. # else
  18. # include <sys/vadvise.h>
  19. # endif
  20.  
  21. # include <stdio.h>
  22. # include <signal.h>
  23. # include <setjmp.h>
  24.  
  25. # define MAXITER    1000000        /* main loop iterations */
  26. # define MAXOBJS    1000        /* objects in pool */
  27. # define BIGOBJ        90000        /* max size of a big object */
  28. # define TINYOBJ    80        /* max size of a small object */
  29. # define BIGMOD        100        /* 1 in BIGMOD is a BIGOBJ */
  30. # define STATMOD    10000        /* interation interval for status */
  31.  
  32. main( argc, argv )
  33. int argc;
  34. char **argv;
  35. {
  36.     register int **objs;        /* array of objects */
  37.     register int *sizes;        /* array of object sizes */
  38.     register int n;            /* iteration counter */
  39.     register int i;            /* object index */
  40.     register int size;        /* object size */
  41.     register int r;            /* random number */
  42.  
  43.     int objmax;            /* max size this iteration */
  44.     int cnt;            /* number of allocated objects */
  45.     int nm = 0;            /* number of mallocs */
  46.     int nre = 0;            /* number of reallocs */
  47.     int nal;            /* number of allocated objects */
  48.     int nfre;            /* number of free list objects */
  49.     long alm;            /* memory in allocated objects */
  50.     long frem;            /* memory in free list */
  51.     long startsize;            /* size at loop start */
  52.     long endsize;            /* size at loop exit */
  53.     long maxiter = 0;        /* real max # iterations */
  54.  
  55.     extern char end;        /* memory before heap */
  56.     char *calloc();
  57.     char *malloc();
  58.     char *sbrk();
  59.     long atol();
  60.  
  61. # ifndef SYS5
  62.     /* your milage may vary... */
  63.     vadvise( VA_ANOM );
  64. # endif
  65.  
  66.     if (argc > 1)
  67.         maxiter = atol (argv[1]);
  68.     if (maxiter <= 0)
  69.         maxiter = MAXITER;
  70.  
  71.     printf("MAXITER %d MAXOBJS %d ", maxiter, MAXOBJS );
  72.     printf("BIGOBJ %d, TINYOBJ %d, nbig/ntiny 1/%d\n",
  73.     BIGOBJ, TINYOBJ, BIGMOD );
  74.     fflush( stdout );
  75.  
  76.     if( NULL == (objs = (int **)calloc( MAXOBJS, sizeof( *objs ) ) ) )
  77.     {
  78.         fprintf(stderr, "Can't allocate memory for objs array\n");
  79.         exit(1);
  80.     }
  81.  
  82.     if( NULL == ( sizes = (int *)calloc( MAXOBJS, sizeof( *sizes ) ) ) )
  83.     {
  84.         fprintf(stderr, "Can't allocate memory for sizes array\n");
  85.         exit(1);
  86.     }
  87.  
  88.     /* as per recent discussion on net.lang.c, calloc does not 
  89.     ** necessarily fill in NULL pointers...
  90.     */
  91.     for( i = 0; i < MAXOBJS; i++ )
  92.         objs[ i ] = NULL;
  93.  
  94.     startsize = sbrk(0) - &end;
  95.     printf( "Memory use at start: %d bytes\n", startsize );
  96.     fflush(stdout);
  97.  
  98.     printf("Starting the test...\n");
  99.     fflush(stdout);
  100.     for( n = 0; n < maxiter ; n++ )
  101.     {
  102.         if( !(n % STATMOD) )
  103.         {
  104.             printf("%d iterations\n", n);
  105.             fflush(stdout);
  106.         }
  107.  
  108.         /* determine object of interst and it's size */
  109.  
  110.         r = random();
  111.         objmax = ( r % BIGMOD ) ? TINYOBJ : BIGOBJ;
  112.         size = r % objmax;
  113.         i = r % (MAXOBJS - 1);
  114.  
  115.         /* either replace the object of get a new one */
  116.  
  117.         if( objs[ i ] == NULL )
  118.         {
  119.             objs[ i ] = (int *)malloc( size );
  120.             nm++;
  121.         }
  122.         else
  123.         {
  124.             /* don't keep bigger objects around */
  125.             if( size > sizes[ i ] )
  126.             {
  127.                 objs[ i ] = (int *)realloc( objs[ i ], size );
  128.                 nre++;
  129.             }
  130.             else
  131.             {
  132.                 free( objs[ i ] );
  133.                 objs[ i ] = (int *)malloc( size );
  134.                 nm++;
  135.             }
  136.         }
  137.  
  138.         sizes[ i ] = size;
  139.         if( objs[ i ] == NULL )
  140.         {
  141.             printf("\nCouldn't allocate %d byte object!\n", 
  142.                 size );
  143.             break;
  144.         }
  145.     } /* for() */
  146.  
  147.     printf( "\n" );
  148.     cnt = 0;
  149.     for( i = 0; i < MAXOBJS; i++ )
  150.         if( objs[ i ] )
  151.             cnt++;
  152.  
  153.     printf( "Did %d iterations, %d objects, %d mallocs, %d reallocs\n",
  154.         n, cnt, nm, nre );
  155.     printf( "Memory use at end: %d bytes\n", sbrk(0) - &end );
  156.     fflush( stdout );
  157.  
  158.     /* free all the objects */
  159.     for( i = 0; i < MAXOBJS; i++ )
  160.         if( objs[ i ] != NULL )
  161.             free( objs[ i ] );
  162.  
  163.     endsize = sbrk(0) - &end;
  164.     printf( "Memory use after free: %d bytes\n", endsize );
  165.     fflush( stdout );
  166.  
  167.     if( startsize != endsize )
  168.         printf("startsize %d != endsize %d\n", startsize, endsize );
  169.  
  170.     free( objs );
  171.     free( sizes );
  172.  
  173.     malloc_dump(2);
  174.     exit( 0 );
  175. }
  176.  
  177.